
import numpy as np
import pandas as pd
from sqlalchemy import create_engine
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d.axes3d import Axes3D # 3D引擎

data = pd.read_excel('C:/Users/Administrator/Desktop/车贷违约预测.xlsx')
# print(data.describe(include="all"))

# for i in data.columns:
# print(data.isnull().sum())
print(data.shape)
data = data.dropna(axis=0)
print(data.shape)

from sklearn.linear_model import LogisticRegression as LR
from sklearn.model_selection import train_test_split
Y=data.pop('是否违约')
X = data
Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,test_size=0.3,random_state=420)
from sklearn.model_selection import GridSearchCV #网格搜索
p = {
    # 'C':list(np.linspace(0.05,1,19)),
    'solver':['liblinear','sag','newton-cg','lbfgs']
}

model = LR(penalty='l2',max_iter=10000)

GS = GridSearchCV(model,p,cv=5)
GS.fit(Xtrain,Ytrain)
print(GS.best_score_)
print(GS.best_params_)
model = LR(penalty='l2',
           max_iter=10000,
           # C=GS.best_params_['C'],
           solver=GS.best_params_['solver'])
model.fit(Xtrain,Ytrain)
print(model.score(Xtrain,Ytrain),model.score(Xtest,Ytest))